var allPro = null;
var allBase = null;
$(function () {
    $.when( $.get(baseURL + 'bss/dpoint/select?dpGuid='+ GetUrlParam("dpGuid")+'&pGuid='+GetUrlParam("pGuid"), function (r) {
            allPro = r.returnlist;
		}),
		$.get(baseURL + 'bss/apbase/select?ourid=isok', function (r) {
			allBase = r.returnlist;
		})

    ).done(
    $("#jqGrid").jqGrid({
        url: baseURL + 'bss/dmaterialbuy/list?dpGuid='+ GetUrlParam("dpGuid")+'&pGuid='+GetUrlParam("pGuid"),
        datatype: "json",
        colModel: [
            { label: 'guid', name: 'guid', index: 'guid', width: 50, key: true,hidden:true },
            {hidden:true, label: '所属项目ID', name: 'pGuid', index: 'p_guid', width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    for (d in allBase) {
                        if (cellvalue == allBase[d].guid) return allBase[d].pBName;
                    }
                }
            },
            { hidden:true,label: '安装位置', name: 'pPGuid', index: 'p_p_guid', width: 80,
                formatter: function (cellvalue, options, rowObject) {
                    for (d in allPro) {
                        if (cellvalue == allPro[d].guid) return allPro[d].pPCode;
                    }
                }
            },
            { hidden:true,label: '关联材料信息库ID', name: 'mMGuid', index: 'm_m_guid', width: 80,hidden:true },
            { hidden:true,label: '材料大类', name: 'mBBigtype', index: 'm_b_bigtype', width: 80 },
            { label: '材料类型', name: 'mBMidtype', index: 'm_b_midtype', width: 80 },
            { label: '材料标签', name: 'dLabel', index: 'd_label', width: 80 },
			{ label: '材料名称', name: 'mBName', index: 'm_b_name', width: 80 },
            { label: '主要参数', name: 'mBParameter', index: 'm_b_parameter', width: 80 },
            { label: '单重（kg）', name: 'mBWeight', index: 'm_b_weight', width: 80 },
            { label: '单价（元）', name: 'mBPrice', index: 'm_b_price', width: 80 },
            { label: '购置单位', name: 'mBUnit', index: 'm_b_unit', width: 80 },
            { label: '数量', name: 'mBCount', index: 'm_b_count', width: 80 },
            { label: '合重（kg）', name: 'mBWeightall', index: 'm_b_weightall', width: 80 },
            { label: '合价（元）', name: 'mBPriceall', index: 'm_b_priceall', width: 80 },
            { label: '数据来源    系统和人工', name: 'mBSource', index: 'm_b_source', width: 80},
            { hidden:true,label: '数据状态', name: 'mBDatastate', index: 'm_b_datastate', width: 80,hidden:true }

        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    })
	)
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
        q:{
            qLabel:null,
            qName:null
        },
		dMaterialBuy: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.dMaterialBuy = {};
		},
		update: function (event) {
			var guid = getSelectedRow();
			if(guid == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(guid)
		},
		saveOrUpdate: function (event) {
			var url = vm.dMaterialBuy.guid == null ? "bss/dmaterialbuy/save" : "bss/dmaterialbuy/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.dMaterialBuy),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var guids = getSelectedRows();
			if(guids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "bss/dmaterialbuy/delete",
                    contentType: "application/json",
				    data: JSON.stringify(guids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(guid){
			$.get(baseURL + "bss/dmaterialbuy/info/"+guid, function(r){
                vm.dMaterialBuy = r.dMaterialBuy;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'qLabel': vm.q.qLabel,
                    'qName': vm.q.qName,
                },
                page:page
            }).trigger("reloadGrid");
		}
	}
});